"use strict";
exports.ISO8601_FORMAT = "yyyy-MM-dd hh:mm:ss.SSS";
exports.ISO8601_WITH_TZ_OFFSET_FORMAT = "yyyy-MM-ddThh:mm:ssO";
exports.DATETIME_FORMAT = "dd MM yyyy hh:mm:ss.SSS";
exports.ABSOLUTETIME_FORMAT = "hh:mm:ss.SSS";

function padWithZeros(vNumber, width) {
  var numAsString = vNumber + "";
  while (numAsString.length < width) {
    numAsString = "0" + numAsString;
  }
  return numAsString;
}

function addZero(vNumber) {
  return padWithZeros(vNumber, 2);
}

/**
 * Formats the TimeOffest
 * Thanks to http://www.svendtofte.com/code/date_format/
 * @private
 */
function offset(timezoneOffset) {
  // Difference to Greenwich time (GMT) in hours
  var os = Math.abs(timezoneOffset);
  var h = String(Math.floor(os / 60));
  var m = String(os % 60);
  if (h.length == 1) {
    h = "0" + h;
  }
  if (m.length == 1) {
    m = "0" + m;
  }
  return timezoneOffset < 0 ? "+" + h + m : "-" + h + m;
}

exports.asString = function( /*format,*/ date, timezoneOffset) {
  var format = exports.ISO8601_FORMAT;
  if (typeof(date) === "string") {
    format = arguments[0];
    date = arguments[1];
    timezoneOffset = arguments[2];
  }
  // make the date independent of the system timezone by working with UTC
  if (timezoneOffset === undefined) {
    timezoneOffset = date.getTimezoneOffset();
  }
  date.setUTCMinutes(date.getUTCMinutes() - timezoneOffset);
  var vDay = addZero(date.getUTCDate());
  var vMonth = addZero(date.getUTCMonth() + 1);
  var vYearLong = addZero(date.getUTCFullYear());
  var vYearShort = addZero(date.getUTCFullYear().toString().substring(2, 4));
  var vYear = (format.indexOf("yyyy") > -1 ? vYearLong : vYearShort);
  var vHour = addZero(date.getUTCHours());
  var vMinute = addZero(date.getUTCMinutes());
  var vSecond = addZero(date.getUTCSeconds());
  var vMillisecond = padWithZeros(date.getUTCMilliseconds(), 3);
  var vTimeZone = offset(timezoneOffset);
  date.setUTCMinutes(date.getUTCMinutes() + timezoneOffset);
  var formatted = format
    .replace(/dd/g, vDay)
    .replace(/MM/g, vMonth)
    .replace(/y{1,4}/g, vYear)
    .replace(/hh/g, vHour)
    .replace(/mm/g, vMinute)
    .replace(/ss/g, vSecond)
    .replace(/SSS/g, vMillisecond)
    .replace(/O/g, vTimeZone);
  return formatted;

};

exports.getMSTime = function(date) {
  var now = date
  if (!(now instanceof Date)) {
    now = new Date()
  }
  return exports.asString(exports.ISO8601_FORMAT, now);
}
exports.getDateTime = function(datetime) {
  var d = new Date(datetime)
  return d.getTime()
}
exports.getRemain = function(start, total, unit) {
  // default ms: 1, sec: 2, min: 3, hou: 4
  var diff = Date.now() + 28800000 - parseInt(this.getDateTime(start)),
    remain = 0;
  total = this.parseMSTimeString(total)
    // if(parseInt(total) && typeof total == 'number'){
    //     total = parseInt(total);
    // } else if(total.indexOf("sec") > -1){
    //     total = parseInt(total.split("sec")[0]) * 1000;
    // } else if(total.indexOf("min") > -1){
    //     total = parseInt(total.split("min")[0]) * 60000;
    // } else if(total.indexOf("hou") > -1){
    //     total = parseInt(total.split("hou")[0]) * 3600000;
    // }
  if (total < 0) {
    return false;
  }
  switch (unit) {
    case 1:
      remain = total - diff;
      break;
    case 2:
      remain = (total - diff) / 1000;
      break;
    case 3:
      remain = (total - diff) / 60000;
      break;
    case 4:
      remain = (total - diff) / 3600000;
      break;
    default:
      remain = (total - diff) / 1000;
      break;
  }
  if (remain > 0) {
    return remain
  }
  return false
}
exports.ZHDateTime = function(datetime) {
  var date = new Date(datetime)
  return date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日";
}
exports.parseMSTimeString = function(timestring) {
  if (parseInt(timestring) && typeof timestring == 'number') {
    return parseInt(timestring);
  } else if (timestring.indexOf("sec") > -1) {
    return parseInt(timestring.split("sec")[0]) * 1000;
  } else if (timestring.indexOf("min") > -1) {
    return parseInt(timestring.split("min")[0]) * 60000;
  } else if (timestring.indexOf("hou") > -1) {
    return parseInt(timestring.split("hou")[0]) * 3600000;
  }
  return 0
}


exports.durationTime = function dutime(time) {
  var sec = 1000,
    min = 60000,
    hou = 3600000,
    day = 86400000,
    _time = 0;
  if (time < sec) {
    return time + " 毫秒";
  }
  if (time < min) {
    return Math.ceil(time / sec) + "秒:" + (time % sec) + "毫秒"
  }
  if (time < hou) {
    _time = time % min; // 余秒
    return Math.ceil(time / min) + "分钟:" + dutime(_time)
  }
  if (time < day) {
    _time = time % hou; // 余分
    return Math.ceil(time / hou) + "小时:" + dutime(_time)
  }
  _time = time % day;
  return Math.ceil(time / day) + "天:" + dutime(_time)
}